//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;

namespace Microsoft.Samples.Workflow.UI.Design
{
    [ProvideProperty("Transitions", typeof(Activity))]
    class TransitionsPropertyExtender : IExtenderProvider
    {
        [SRCategory(SR.Navigation)]
        [SRDescription(SR.TransitionsDescription)]
        [TypeConverter(typeof(TransitionsTypeConverter))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TransitionCollection GetTransitions(Activity activity)
        {
            if (activity.Parent is Navigator)
            {
                return Navigator.GetTransitions(activity) as TransitionCollection;
            }
            else
            {
                return null;
            }
        }

        [SRCategory(SR.Navigation)]
        [SRDescription(SR.TransitionsDescription)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public void SetTransitions(Activity activity, TransitionCollection transitions)
        {
            if (activity.Parent is Navigator)
            {
                TransitionCollection oldList = Navigator.GetTransitions(activity) as TransitionCollection;
                NavigatorDesigner navDesigner = NavigatorDesigner.GetDesigner(activity.Parent) as NavigatorDesigner;
                Navigator.SetTransitions(activity, transitions);

                if (transitions.Count > 0)
                {
                    foreach (Transition oldTransition in oldList)
                    {
                        if (!transitions.Contains(oldTransition.Target))
                        {
                            navDesigner.RemoveConnector(activity, oldTransition.Target);
                        }
                    }
                }
                else
                {
                    // Remove all existing possible transitions.
                    navDesigner.RemoveConnector(activity, null);
                }

                if (transitions.Count > 0)
                {
                    foreach (Transition transition in transitions)
                    {
                        if (!oldList.Contains(transition.Target))
                        {
                            navDesigner.AddConnector(activity, transition.Target);
                        }
                    }
                }

                navDesigner.RefreshLayout();
            }
        }

        public bool CanExtend(object extendee)
        {
            return ((extendee != this) && (extendee is Activity) && (((Activity)extendee).Parent is Navigator));
        }
    }
}
